package ro.xblue.translator;

import java.util.ArrayList;

import org.apache.struts.action.ActionForm;



/**
 * <p>Title: Romanian AIDA</p>
 * <p>Description: :D application</p>
 * <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: Romania Development Gateway </p>
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: TransItemForm.java,v 1.1 2004/10/24 23:37:02 mihaipostelnicu Exp $
 */
public class TransItemForm extends ActionForm
{
      TransItem dest_item;
      TransItem src_item;
      TransItem[] all_langs_items;
      String referer;

      public String getReferer()
      {
            return referer;
      }

      public void setReferer( String referer )
      {
            this.referer = referer;
      }

      public String getSrc_lang()
      {
            if ( src_item == null ) return null;
            return src_item.getLang();
      }

      public String getDest_lang()
      {
            if ( dest_item != null )
            {
                  return dest_item.getLang();
            }
            else
            {
                  return null;
            }
      }

      public TransItemForm()
      {
            dest_item = new TransItem();
      }

      public TransItem[] getAll_langs_items()
      {
            return all_langs_items;
      }

      public void setId( long id )
      {
            this.dest_item.setId( id );
      }

      public void setModule( String module )
      {
            this.dest_item.setModule( module );
      }

      public void setLang( String lang )
      {
            this.dest_item.setLang( lang );
      }

      public void setLabel( String label )
      {
            this.dest_item.setLabel( label );
      }

      public void setText( String text )
      {
            this.dest_item.setText( text );
      }

      public String getModule()
      {
            return this.dest_item.getModule();
      }

      public String getText()
      {
            return this.dest_item.getText();
      }

      public String getLang()
      {
            return this.dest_item.getLang();
      }

      public String getLabel()
      {
            return this.dest_item.getLabel();
      }

      public long getId()
      {
            return this.dest_item.getId();
      }

      public String getSrcText()
      {
            if ( src_item != null )
            {
                  return src_item.getText();
            }
            else
            {
                  return "";
            }
      }

      public void reset_defaults()
      {
            this.dest_item.setId( -1 );
            this.dest_item.setModule( null );
            this.dest_item.setText( null );
            this.dest_item.setLabel( null );
            this.dest_item.setLang( null );
            this.all_langs_items = null;
      }

      public void populate( TransItem item, TransItem[] all_langs_items )
      {
            this.dest_item.setId( item.getId() );
            this.dest_item.setLabel( item.getLabel() );
            this.dest_item.setLang( item.getLang() );
            this.dest_item.setModule( item.getModule() );
            this.dest_item.setText( item.getText() );
            this.all_langs_items = all_langs_items;
      }

      public String[] isValid()
      {
            ArrayList items = new ArrayList();
            String result[] = null;

            if ( dest_item.getModule() == null )
            {
                  items.add( "err.module_req" );
            }
            if ( dest_item.getLabel() == null )
            {
                  items.add( "err.label_req" );
            }
            if ( dest_item.getText() == null )
            {
                  items.add( "err.text_req" );
            }

            if ( items.size() > 0 )
            {
                  result = new String[ items.size() ];
                  items.toArray( result );
            }

            return result;
      }

      public void setSrc_lang( String src_lang )
      {
            TransItem[] items = this.getAll_langs_items();
            if ( items == null ) this.src_item = null;

            for ( int i = 0; i < items.length; i++ )
            {
                  TransItem item = items[ i ];
                  if ( item.getLang().equals( src_lang ) )
                  {
                        this.src_item = item;
                        return;
                  }
            }

            this.src_item = null;
      }

      public void setDest_lang( String dest_lang )
      {
            this.dest_item = null;
            TransItem[] items = this.getAll_langs_items();
            if ( items != null )
            {
                  for ( int i = 0; i < items.length; i++ )
                  {
                        if ( items[ i ].getLang().equals( dest_lang ) )
                        {
                              this.dest_item = items[ i ];
                        }
                  }
            }

            if ( dest_item == null )
            {
                  dest_item = new TransItem();
                  if ( items != null )
                  {
                        dest_item.setModule( items[ 0 ].getModule() );
                        dest_item.setLabel( items[ 0 ].getLabel() );
                        dest_item.setLang( dest_lang );
                  }
                  else
                  {
                        dest_item = new TransItem();
                        dest_item.setLang( dest_lang );
                  }
            }
      }
}
